﻿using System.Collections.Generic;
using System.Text;
using AppToolKit.Core.Context.Spring;
using PMSBusiness.Contract.Background;
using Common.Logging;
using PMSBusiness.Core.Logger;


namespace PMSBusiness.Service.Background
{
    [AutoWiring]
    public class WorkTaskExecutor
    {
        public Dictionary<string,WorkTaskSetBase> dctWorkSet = new Dictionary<string,WorkTaskSetBase>();

        [AutoWiring]
        public ContextAware Context { get; set; }

        private static ILog logger_ = LogManager.GetLogger(LoggerFixure.LoggerConfigure.DefaultLogger);

        public WorkTaskExecutor()
        {

        }

        public void Excute()
        {
            for (int i = 0; i < dctWorkSet.Count; i++)
            {
                foreach (KeyValuePair<string, WorkTaskSetBase> kvp in dctWorkSet)
                {
                    kvp.Value.Execute();
                }

                //WorkTaskSetBase aWorkSet =  dctWorkSet.Values[i];
                //aWorkSet.Execute();
            }

        }

        public void Excute(string workSet)
        {
           if(dctWorkSet.ContainsKey(workSet))
           {
               dctWorkSet[workSet].Execute();
           }
            else
            throw new WorkTaskExecption(string.Format("workSet({0})不存在",workSet));
        }


        /// <summary>
        /// 添加一个workset，返回这个workset的位置
        /// </summary>
        /// <param name="workTaskSet"></param>
        /// <returns></returns>
        public int Add(WorkTaskSetBase workTaskSet)
        {
            dctWorkSet.Add(workTaskSet.Name, workTaskSet);
            return dctWorkSet.Count - 1;
        }


    }
}
